package by.gamedev.site.client.app.place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.requestfactory.shared.EntityProxy;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;

/**
 * Abstract implementation of ProxyListView.
 * 
 * @param <P>
 *            the type of the proxy
 */
public abstract class AbstractProxyListView<P extends EntityProxy> extends Composite implements ProxyListView<P> {

    private HasData<P>        display;
    ProxyListView.Delegate<P> delegate;

    @Override
    public HasData<P> asHasData() {
        return display;
    }

    @Override
    public AbstractProxyListView<P> asWidget() {
        return this;
    }

    @Override
    public void setDelegate(final Delegate<P> delegate) {
        this.delegate = delegate;
    }

    protected void init(final Widget root, final HasData<P> displayData, final Button newButton) {
        super.initWidget(root);
        this.display = displayData;

        newButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(final ClickEvent event) {
                delegate.createClicked();
            }
        });
    }

    @Override
    protected void initWidget(final Widget widget) {
        throw new UnsupportedOperationException("AbstractRecordListView must be initialized via " + "init(Widget, HasData<P>, Button) ");
    }
}
